<template>
<div style="height: 600px; background-color: #f6f7f9">
  <!-- 顶部导航 -->
  <van-sticky>
    <van-nav-bar title="地址管理" left-arrow @click-left="$router.go(-1)">
    </van-nav-bar>
  </van-sticky>
  <van-address-list
      v-model="chosenAddressId"
      :list="list"
      default-tag-text="默认"
      @add="onAdd"
      @edit="onEdit"
      @select="choseAddress"
  />
</div>
</template>

<script>
export default {
  name: "Address",
  data() {
    return {
      chosenAddressId: '1',
      list: [
        {
          id: '1',
          name: '张三',
          tel: '13000000000',
          address: '浙江省杭州市西湖区文三路 138 号东方通信大厦 7 楼 501 室',
          isDefault: true,
        },
      ],
    };
  },
  methods: {
    showAddress(){
      console.log('在localStorage中的JWT数据：' + localStorage.getItem('jwt'));
      let url = 'http://localhost:9080/addresses';
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        console.log(response.data.data);
        let responseBody=response.data.data;
        this.list = responseBody;
      })
    },
    onAdd() {
      this.$router.push('/user/address-add');
    },
    onEdit(item) {
      console.log(item)
      localStorage.setItem("addressId",item.id);
      this.$router.push('/user/address-update');
    },
    choseAddress(item){
      let addressInfoString = JSON.stringify(item);
      localStorage.setItem("addressInfo",addressInfoString);
      this.$router.push('/home/order-add');
    }
  },
  mounted() {
    this.showAddress();
  }
}
</script>

<style scoped>

</style>
